package ge.telasi.tasks.ui.comp;

import javax.swing.JLabel;

/**
 * @author dimitri
 */
public class AnimatedLabel extends JLabel {
    private static final long serialVersionUID = -7856271195626288567L;
    private String baseText;
    private String color1 = "#555555";
    private String color2 = "#ff8855";
    private int index;

    public AnimatedLabel(String text) {
        setBaseText(text);
        new Thread(new MyAnimation()).start();
    }

    public void setBaseText(String baseText) {
        this.baseText = baseText;
    }

    public String getBaseText() {
        return baseText;
    }

    class MyAnimation implements Runnable {
        public void run() {
            while(true) {
                int length = baseText.length();
                if (index > length - 3)
                    index = 0;
                else
                    index ++;
                StringBuilder b = new StringBuilder();
                b.append("<html>");
                b.append("<font color=\"");
                b.append(color1);
                b.append("\">");
                
                b.append(baseText.substring(0, index));
                b.append("</font>");

                b.append("<font color=\"");
                b.append(color2);
                b.append("\">");
                b.append(baseText.substring(index, index + 2));
                b.append("</font>");

                b.append("<font color=\"");
                b.append(color1);
                b.append("\">");
                b.append(baseText.substring(index + 2));
                b.append("</font>");

                b.append("</html>");
                AnimatedLabel.this.setText(b.toString());
                try {
                    Thread.sleep(100);
                } catch (Exception ex) {}
            }
        }
    }

}
